package com.fbms.module.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbms.common.utils.StreamKit;
import com.fbms.module.common.ICheckFieldService;
import com.fbms.module.system.bean.dto.SysMenuDTO;
import com.fbms.module.system.bean.dto.SysRoleDTO;
import com.fbms.module.system.bean.entity.SysRole;
import com.fbms.module.system.bean.entity.SysRoleMenu;
import com.fbms.module.system.dao.SysRoleMapper;
import com.fbms.module.system.dao.SysRoleMenuMapper;
import com.fbms.module.system.service.ISysMenuService;
import com.fbms.module.system.service.ISysRoleMenuService;
import com.fbms.module.system.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：角色实现类
 *
 * @ClassName SysRoleServiceImpl
 * @Author HappyBoy
 * @Date 2023-03-21 16:52
 **/
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    /**
     * 角色Mapper
     */
    public final SysRoleMapper roleMapper;

    /**
     * 菜单服务类
     */
    private final ISysMenuService menuService;

    /**
     * 角色菜单服务类
     */
    private final ISysRoleMenuService roleMenuService;

    /**
     * 校验字段服务类
     */
    private final ICheckFieldService checkFieldService;
    /**
     * 获取分页角色信息
     * @param page 分页信息
     * @return 分页信息
     */
    @Override
    public IPage<SysRoleDTO> getRolePageList(SysRole role, IPage<SysRole> page) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>(role);
        IPage<SysRole> sysRoleIPage = roleMapper.selectPage(page, queryWrapper);
        return sysRoleIPage.convert(u -> {
            SysRoleDTO v = new SysRoleDTO();
            BeanUtils.copyProperties(u, v);
            // 填充菜单
            String roleId = u.getId();
            List<SysMenuDTO> menuList = menuService.getMenuListByRoleId(roleId);
            v.setMenuList(menuList);
            return v;
        });
    }

    /**
     * 保存角色信息
     * @param roleInfo 角色
     * @param menus 菜单
     * @return 条数
     */
    @Transactional
    @Override
    public Boolean saveRole(SysRole roleInfo, List<String> menus) {
        // 1.保存角色
        boolean roleFlag = saveOrUpdate(roleInfo);
        // 2.获取角色ID 保存角色菜单表
        String roleId = roleInfo.getId();
        List<SysRoleMenu> roleMenuList = new ArrayList<>(menus.size());
        for (String menuId : menus){
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuList.add(roleMenu);
        }
        boolean roleMenuFlag = roleMenuService.saveBatch(roleMenuList, menus.size());
        return roleFlag && roleMenuFlag;
    }

    /**
     * 检查角色名是否重复
     * @param roleName 角色名称
     * @return 0-否 1-是
     */
    @Override
    public String checkRoleNameUnique(String roleName) {
        return checkFieldService.checkFieldUnique(roleMapper, SysRole::getName, roleName);
    }

    /**
     * 更新角色信息
     * @param roleId 角色Id
     * @param roleInfo 角色信息
     * @return 是否成功
     */
    @Transactional
    @Override
    public Boolean updateRole(String roleId, SysRoleDTO roleInfo) {
        // 1、根据角色Id删除中间表信息
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setRoleId(roleId);
        roleMenuService.removeById(roleMenu);
        // 2、保存
        return saveRole(roleInfo, StreamKit.mapToList(roleInfo.getMenuList(), SysMenuDTO::getId));
    }


}
